/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import BE.Recomendacion;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;

import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author CarLita
 */
public class RecomendacionDao {
        Session session;

    public boolean guardar(Recomendacion recomendacion) {
        boolean ok = true;
        session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            tx.begin();
            session.saveOrUpdate(recomendacion);
            tx.commit();
        } catch (HibernateException he){
            tx.rollback();
            he.printStackTrace();
            ok=false;
        } finally {
            session.close();
        }
        return ok;
    }

    public Recomendacion buscarPorId(int idRecomendacion) {
            session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Recomendacion recomendacion where recomendacion.idRecomendacion = ? ";
        Query query = session.createQuery(hql);
        query.setInteger(0, idRecomendacion);
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return (Recomendacion)resultList.get(0);
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }
    
      public List buscarPorCriterios(Recomendacion recomendacion) {
        session = HibernateUtil.getSessionFactory().openSession();        
        

        String hql = "from Recomendacion recomendacion where "
                + "recomendacion.titulo like concat('%',?,'%') "      
                + "and recomendacion.recomendacion like concat('%',?,'%') ";        
        /*if (recomendacion.getScore()>0){
            hql+= "and recomendacion.score = ?";
        }*/
                
        Query query = session.createQuery(hql);
        query.setString(0, ""/*recomendacion.getTitulo()*/);
        query.setString(1, recomendacion.getRecomendacion());
        /*if (recomendacion.getScore()>0){
            query.setInteger(2, recomendacion.getScore());
        }*/             
        
        List resultList = null;

        try {            
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
        } catch (HibernateException he) {
            he.printStackTrace();            
        } finally {
            session.close();
        }
        return resultList;
    }

    public boolean eliminar(Recomendacion r){
        boolean ok =true;
        
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.delete(r);
            tx.commit();
            session.close();
        }
        catch(Exception e){
            ok = false;
            System.out.println(e.getMessage());
        }
        return ok;
    }
}
